﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SysManager_UsersModuleRights : BaseDataPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(Page_Load);
    } 

    protected  void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetUsers();
            IniModleTree(this.TreeView2);
        }
    }

    private void GetUsers()
    {
        this.GridView1.DataSource = BinUserList("");
        this.GridView1.DataBind();
    }
    protected void TreeView2_SelectedNodeChanged(object sender, EventArgs e)
    {
        bindldyout(this.TreeView2.SelectedNode.Value);
        this.Label1.Text = "当前节点：" + this.TreeView2.SelectedNode.Text;
    }
    private void bindldyout(string mid)
    {
        if (string.IsNullOrEmpty(this.hf_UserID.Value))
        {
            return;
        }
        string sql = @"select mu_id,lindx,fieldname,fielddisplay,mu_visible,fieldwidth from sys_moduleslayout a 
                    inner join sys_modulesuserlayout b on b.fid =lid 
                    where a.mid = " +mid
                  + " and userid=" + this.hf_UserID.Value;
        this.GridView2.DataSource = mydb.DM_GetDateSet(sql);
        this.GridView2.DataBind();
        DataSet ds = mydb.DM_GetDateSet("select * from sys_modulerights where mid ="+mid+" and userid="+this.hf_UserID.Value);
        if (ds.Tables[0].Rows.Count > 0)
        {
            this.M_Add.Checked = (ds.Tables[0].Rows[0]["M_Add"].ToString().ToLower() == "true" ? true : false);
            this.M_Delete.Checked = (ds.Tables[0].Rows[0]["M_Delete"].ToString().ToLower() == "true" ? true : false);
            this.M_Edit.Checked = (ds.Tables[0].Rows[0]["M_Edit"].ToString().ToLower() == "true" ? true : false);
            this.M_Browse.Checked = (ds.Tables[0].Rows[0]["M_Browse"].ToString().ToLower() == "true" ? true : false);
            this.M_Export.Checked = (ds.Tables[0].Rows[0]["M_Export"].ToString().ToLower() == "true" ? true : false);
        }
        else
        {
            this.M_Add.Checked = false;
            this.M_Delete.Checked = false;
            this.M_Edit.Checked = false;
            this.M_Browse.Checked = false;
            this.M_Export.Checked = false;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        hf_UserID.Value = e.CommandArgument.ToString();
        if (TreeView2.SelectedNode == null)
        {
            return;
        }
        bindldyout(this.TreeView2.SelectedNode.Value);
    }
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView2.EditIndex = e.NewEditIndex;
        bindldyout(this.TreeView2.SelectedNode.Value);
    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;

        bindldyout(this.TreeView2.SelectedNode.Value);
    }
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string orderid = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();//第一列
        string fieldname = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();//第二列
        string displayname = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();//第三列
        string fieldwidth = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();//第四列
        string mu_visible = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();//第五列
        string mu_id= GridView2.DataKeys[e.RowIndex].Value.ToString();
        
        string[] sql={"",""};

        sql[0] = "update sys_modulesuserlayout set mu_visible=" + (mu_visible.ToLower()=="true"?1:0)
            + " where mu_id=" + mu_id;
        sql[1] = "update sys_moduleslayout set lindx="+orderid+",fieldname='"+fieldname
            +"',fielddisplay='"+displayname+"',fieldwidth= " + fieldwidth
            + " where lid in(select fid from sys_modulesuserlayout where mu_id =" + mu_id + ")";
        if (mydb.ExecuteNonQueryTran(sql))
        {
            ShowMessage(Response, "修改成功！");
        }else{
            ShowMessage(Response, "修改失败！");
        }
        GridView2.EditIndex = -1;
        bindldyout(this.TreeView2.SelectedNode.Value);        
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.TreeView2.SelectedValue))
        {
            return;
        }
        string modetype = mydb.DM_GetExecuteScalarObj("select moduletype from sys_modules where mid =" + this.TreeView2.SelectedValue).ToString();
        if (modetype != "1")
        {
            ShowMessage(Response,"当前节点不是功能菜单，无法设置权限！");
            return;
        }
        string sql = "";
        CheckBox chkbox = (CheckBox)sender;
        sql = @" insert into sys_modulerights(userid,mid,creator,M_Browse)
                    select "+hf_UserID.Value+@",mid,"+Session["UserID"]+@",0 from Sys_Modules where ModuleType =1 and 
                    mid not in (select Mid from Sys_ModuleRights where userid ="+hf_UserID.Value+@")
                if not exists(select * from sys_modulerights where mid=" + TreeView2.SelectedValue+" and userid="+hf_UserID.Value+@")
                    insert into sys_modulerights(userid,mid,creator,M_Browse)values("
                    +hf_UserID.Value+","+TreeView2.SelectedValue+",'"+Session["UserID"]+@"',1)
                else
                    update sys_modulerights set " + chkbox.ID+"="+(chkbox.Checked?1:0)+",modifydate='"+DateTime.Now.ToString()+"' where mid="
            +this.TreeView2.SelectedValue+" and userid="+this.hf_UserID.Value;
        mydb.DM_ExecuteNonQuery(sql);
    }
    protected void SaveBtnChecked_Click(object sender, EventArgs e)
    {

    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        mydb.DM_ExecuteNonQuery("delete from sys_modulesuserlayout where mu_id=" + GridView2.DataKeys[e.RowIndex].Value.ToString());
        bindldyout(this.TreeView2.SelectedNode.Value); 
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#d3d7d4'"); //鼠标移进
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor"); //鼠标移出
            e.Row.Attributes.Add("onclick", "if(window.oldtr!=null）{window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#d3d7d4';window.oldtr=this");    //点击行 
        }
    }
}